<?php
$auth = Auth::getAuth('current_user');

$db = new Database;
$fs = new functions;
$search = new Search();
$myrequest = new MyRequest();
$userID = $auth['id'];
 if(isset($_POST['action'])){

     $date = $fs->currentDateTime();

     switch($_POST['action']){
        
        case "loadData":
               $returnJson = array();
               $search_value = $_POST['search_value'];
               $forms_array = $search->getCompaniesForm("AND is_active = 1");
               $start = $_POST['start'];
               // $start = 0;
               $countRecord = 0;
               $unionQuery = "";
               $ctr = 0;
               $limit = "";
               foreach ($forms_array as $forms) {
                  $id =  $forms['id'];
                  $form_tablename = $forms['form_table_name'];
                  $sqlStr = "SELECT count(*) as count FROM information_schema.tables WHERE table_schema = '". DB_NAME ."' AND table_name = '". $form_tablename ."'";
                  $query = $this->query($sqlStr,"row");

                  if($query['count']>0){
                    $unionQuery.=$myrequest->setQuery($form_tablename,$id,$search_value);
                    // if($ctr<count($forms_array)-1){
                      $unionQuery.=" UNION ALL ";
                    // }
                  }
                  // $ctr++;
                }
                if($start!=""){
                  $limit = " LIMIT $start, 10";
                }
                $unionQuery = substr($unionQuery, 0,strlen($unionQuery)-11);
                $unionQuery =  $unionQuery.$limit; 
                // echo $unionQuery;
                $return = $db->query($unionQuery,"array");
                foreach ($return as $request) {
                    $request['user_image'] = post::avatarPic("tbuser",$request['requestor_id'],"30","30","small","avatar");
                    array_push($returnJson, $request);
                }
              echo json_encode($returnJson);
              // echo count($returnJson);
            break;
        case "loadDataDatatable":
              
              $returnJson = array();
               $search_value = $_POST['search_value'];
               $forms_array = $search->getCompaniesForm("AND is_active = 1");
               $start = $_POST['iDisplayStart'];
               // $start = 0;
               $countRecord = 0;
               $unionQuery = "";
               $ctr = 0;
               $limit = "";
               // $countTotal = MyRequest::countMyRequest();
               foreach ($forms_array as $forms) {
                  $id =  $forms['id'];
                  $form_tablename = $forms['form_table_name'];
                  $sqlStr = "SELECT count(*) as count FROM information_schema.tables WHERE table_schema = '". DB_NAME ."' AND table_name = '". $form_tablename ."'";
                  $query = $this->query($sqlStr,"row");

                  if($query['count']>0){
                    $unionQuery.=$myrequest->setQuery($form_tablename,$id,$search_value);
                    // if($ctr<count($forms_array)-1){
                      $unionQuery.=" UNION ALL ";
                    // } 
                  }
                  // $ctr++;
                }
                if($start!=""){
                  $limit = " LIMIT $start, 10";
                }
                $orderBy = " ORDER BY DateCreated DESC";
                if($_POST['column-sort']){
                  $orderBy = " ORDER BY ".$_POST['column-sort']." ".$_POST['column-sort-type'];
                }
                $unionQuery = substr($unionQuery, 0,strlen($unionQuery)-11);
                $countTotal = $db->query($unionQuery,"numrows");
                $unionQuery =  $unionQuery.$orderBy.$limit; 

                // echo $unionQuery;
                $output = array(
                    "sEcho" => intval($_POST['sEcho']),
                    "iTotalRecords" => $countTotal,
                    "iTotalDisplayRecords" => $countTotal,
                    "start"=>$start,
                    "aaData" => array(),
                    "column-sort" => $_POST['column-sort'],
                    "column-sort-type" => $_POST['column-sort-type'],
                );
                $return = $db->query($unionQuery,"array");
                foreach ($return as $request) {
                    $aaData = array();
                    $request['user_image'] = post::avatarPic("tbuser",$request['requestor_id'],"30","30","small","avatar");
					$button = '<a class="linkTrigger display" href="/user_view/workspace?view_type=request&formID=' . $request['form_id'] . '&requestID=' . $request['request_id'] . '&trackNo=' . $request['TrackNo'] . '"></a>';
                    array_push($aaData, $request['form_name']." ".$button);
                    // array_push($returnJson, $request);
                    //array_push($aaData, $request['form_name']);
                    array_push($aaData, $request['TrackNo']);
                    array_push($aaData, $request['Status']);
                    array_push($aaData, $request['DateCreated']);
                    $output['aaData'][] = $aaData;
                }
              echo json_encode($output);
              break;
        case 'countData':
              echo $myrequest->countMyRequest();
              break;
	}
    
 }
class MyRequest extends Database{

    public function setQuery($form_tablename,$id,$search_value){
      $auth = Auth::getAuth('current_user');
      $userID = $auth['id'];
      $query_str = "(SELECT form.DateCreated as DateCreated, form.TrackNo as TrackNo,form.id as request_id, u.first_name,u.last_name,u.middle_name, form.Status as Status,
                    form.Requestor as requestor_id, form.Processor as processors_id, tbw.form_name, tbw.id as form_id 
                    FROM `$form_tablename` form LEFT JOIN tbuser u ON u.id=form.requestor, tb_workspace tbw
                    WHERE form.requestor = '$userID' AND tbw.id = $id AND (tbw.form_name LIKE '%". $search_value ."%' OR form.TrackNo LIKE '%". $search_value ."%' OR form.Status LIKE '%". $search_value ."%') ORDER BY form.id DESC) ";
      return $query_str;                    
    }

    // public function countMyRequest(){
    //   $search = new Search();
    //   $ctr = 0;
    //   $forms_array = $search->getCompaniesForm();
    //   foreach ($forms_array as $forms) {
    //     $id =  $forms['id'];
    //     $request_array = $search->countOwnRequest($id);
    //     $ctr+=$request_array;
    //   }
    //   return $ctr;
    // }
    public function countMyRequest(){
      //$search = new Search();
      //$ctr = 0;
      //$unionQuery = "";
      //$forms_array = $search->getCompaniesForm("AND is_active = 1");
      //$search_value = "";
      //foreach ($forms_array as $forms) {
      //  $id =  $forms['id'];
      //  $form_tablename = $forms['form_table_name'];
      //  $unionQuery .= $this->setQuery($form_tablename,$id,$search_value);
      //  if($ctr<count($forms_array)-1){
      //    $unionQuery.=" UNION ALL ";
      //  }
      //  $ctr++;
      //}
      //// echo $unionQuery;
      //$countTotal = $this->query($unionQuery,"numrows");
      //// return $ctr;
      //return $countTotal;
      return "0";
    }
}
?>